 #include "playpage.h"
#include "ui_playpage.h"
#include<QPainter>
#include<QPixmap>
#include<QIcon>
#include<QTimer>
#include"runner.h"
#include"obstacle.h"
#include<QDebug>
#include<QMediaPlayer>
#include<QMediaPlaylist>
int playpage::x1=0;
int playpage::x2=1000;
int playpage::y=550;
int playpage::index=0;

playpage::playpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::playpage)
{
    ui->setupUi(this);
    setFixedSize(1000,700);
    setWindowTitle("Stick Runner");
    setWindowIcon(QIcon(":/image/Image/title.png"));
    for(int i=0;i<20;i++){
        barrier1[i].initimage1();
    }
    for(int i=0;i<20;i++){
        barrier2[i].initimage2();
    }
    timer->start(80);
    over=false;
    difficulty=0;
    groundy=650;
    connect(timer,&QTimer::timeout,[=](){//让地面和背景随着时间移动
        if(x1==-1000) x1=1000;
        if(x2==-1000) x2=1000;
        x1-=20;
        x2-=20;
        if(y<550){
            y+=25;
        }
        update();
    });
    connect(timer,&QTimer::timeout,[=](){
        if(index==4) index=0;
       index++;
       if(difficulty<=70){
           difficulty+=0.002;
       }
       addObstacle();
       for(int i=0;i<20;i++){
           if(!barrier1[i].free){
               barrier1[i].befree();
           }
       }
       for(int i=0;i<20;i++){
           if(!barrier2[i].free){
               barrier1[i].befree();
           }
       }
       update();
       for(int i=0;i<20;i++){
           barrier1[i].move();
           if(barrier1[i].isCollision(y)) over=true;
           break;
       }
//       for(auto i=barrier.begin();i!=barrier.end();i++){
//           if((*i)->done()){
//               i=barrier.erase(i);
//           }
//           else if((*i)->isCollision(y+5)){
//               gameover();
//           }
//           if(!barrier.empty()){
//               (*i)->move();
//           }
//       }
    });
}
void playpage::paintEvent(QPaintEvent *){
    QPainter painter(this);
    bg1.load(":/background/Image/backg.jpg");
    bg2.load(":/background/Image/backg.jpg");
    gd1.load(":/background/Image/ground.png");
    gd2.load(":/background/Image/ground.png");
    painter.drawPixmap(x1,0,1000,700,bg1);//跑步时的背景
    painter.drawPixmap(x2,0,1000,700,bg2);
    painter.drawPixmap(QRect(x1,650,1000,50),gd1);//画地面
    painter.drawPixmap(QRect(x2,650,1000,50),gd2);
    QRect q1(0,0,0,0);
    QRect q2(40,y,80,100);
    if(y==550){
    painter.drawPixmap(q2,hero.run[index],q1);
    }
    if(y<550){
    painter.drawPixmap(q2,hero.jump,q1);
    }
    for(int i=0;i<20;i++)
    {
        if(!barrier1[i].free){
            painter.drawPixmap(QRect(barrier1[i].getX(),barrier1[i].getY(),barrier1[i].getWidth(),barrier1[i].getHeight()),barrier1[i].getImg());
            update();
            break;
        }
    }
    for(int i=0;i<20;i++){
        if(!barrier2[i].free){
            painter.drawPixmap(QRect(barrier2[i].getX(),barrier2[i].getY(),barrier2[i].getWidth(),barrier2[i].getHeight()),barrier2[i].getImg());
            update();
            break;
        }
    }
    if(over){
        painter.drawPixmap(0,0,1000,700,QPixmap(":/image/Image/gameover.jpg"));
    }
    update();
}
void playpage::keyPressEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_K:{
        if(y==550) y-=300;
        if(250<=y&&y<550) y-=200;
        break;
    }
    }
}
void playpage::gameover(){
    over=true;
}
void playpage::addObstacle(){
    for(int i=0;i<20;i++){
        if(barrier1[i].free){
            srand(time(NULL));
            int x=1000;
            int h=(rand()%101)+100;
            int y1=650-h;
            int w=10;
            barrier1[i].initWall(x,y1,w,h);
            barrier1[i].free=false;
            break;
        }
    }
    for(int i=0;i<20;i++){
        if(barrier2[i].free){
            barrier2[i].initimage2();
            srand(time(NULL));
            int x=1000;
            int h=(rand()%101)+100;
            int y1=650-h;
            int w=10;
            barrier2[i].initWall(x,y1,w,h);
            barrier2[i].free=false;
            break;
        }
    }
}
playpage::~playpage()
{
    delete ui;
}
